/**
 * 
 */
package com.netscenery.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import java.io.OutputStreamWriter;


/**
 * @author llly
 *
 */
public class FileUtil {
	/**
	 * �ļ����������Դ��Ŀ�Ķ����ļ�������Դ����Ŀ��
	 * ���Դ��Ŀ�Ķ���Ŀ¼�����ԴĿ¼�µ������ļ���������Ŀ��Ŀ¼��
	 * ���Դ���ļ���Ŀ����Ŀ¼�����Դ�ļ�������Ŀ��Ŀ¼��
	 * ���Դ��Ŀ¼����Ŀ�����ļ����򱨴�
	 * @param src
	 * @param dst
	 * @return
	 * @throws IOException 
	 */
	public static boolean copy(final File src,File dst) throws IOException{
		if (null == src){
			return false;
		}
		
		if (null == dst){
			return false;
		}
		//Դ�ļ��������
		if (!src.exists()){
			return false;
		}
		if (!dst.exists()){
			dst.createNewFile();
		}
		
		if (src.isDirectory() && dst.isFile()){
			return false;
		}
		
		
		
		if (src.isFile() && dst.isFile()){
			copyFile(src, dst);
		}
		else if (src.isFile()&& dst.isDirectory()){
			File dstFile = new File(dst,src.getName());
			copyFile(src, dstFile);
		}
		else if (src.isDirectory() && dst.isDirectory()){
			File[] files = src.listFiles();
			int size = files.length;
			for (int i = 0; i < size; i++){
				File file = files[i];
				if (null != file){
					copy(file, dst);
				}
			}
		}
		else {
			return false;
		}
		
		return true;
		
	}
	
	 public static boolean copyFile(File src, File dst) throws IOException
	    {
	        if (src == null || dst == null) {
	        	return false;
	        }
	        if (src.isDirectory() || dst.isDirectory()){
	        	return false;
	        }
	        //�Ȱ�Դ�ļ����ݶ�ȡ����
	        String srcString = readFile2String(src);
	        File dstBak= null;
	        if (dst.exists()){
	        	//�ȰѾɵ��ļ�����һ��			 
	        	dstBak = new File(dst.getName()+".bak");
				dst.renameTo(dstBak);
				//ɾ����ɵ��ļ�
				dst.delete();
				//����µĿ��ļ�
				dst.createNewFile();
	        }
	        
	        
	       
	       if (writeUtf8File(srcString, dst)){
	    	   if (null != dstBak){
	    		   dstBak.delete();
	    	   }
	    	   return true;
	       }
	       else {
	    	   if (null != dstBak){
	    		   dstBak.renameTo(dst);
	    		   dstBak.delete();
	    	   }
	    	   return false;
	       }
	    }

	public static String readFile2String(String path) throws IOException{
		if (null == path){
			return null;
		}
		return readFile2String(new File(path));
	    
	}
	
	public static String readFile2String(File file) throws IOException{
	    if (null == file){
	    	return null;
	    }
	    if(!file.exists()||file.isDirectory()){
	    	System.out.println("File:" + file.getName() + " path:" + file.getAbsolutePath());
	    	throw new FileNotFoundException();
	    }
	    InputStreamReader read = new InputStreamReader(new FileInputStream(
				file), "UTF-8");    
	    BufferedReader br=new BufferedReader(read);
	    String temp=null;
	    StringBuffer sb=new StringBuffer();
	    temp=br.readLine();
	    while(temp!=null){
	        sb.append(temp+"\n");
	        temp=br.readLine();
	    }
	    br.close();
	    return sb.toString();
	}

	public static void save2File(String buffer, String out){
		FileWriter fw = null;
		try {
			fw = new FileWriter(out);
			try {
				fw.write(buffer);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				fw.close();
			}
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return;
	}

	public static boolean save2File(String buffer, File out){
		FileWriter fw = null;
		buffer = buffer.replaceAll("\\\\\\\\", "\\\\");
		buffer = removeUTF8BOM(buffer);
		try {
			fw = new FileWriter(out);
			try {
				fw.write(buffer);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				fw.close();
				return false;
			}
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	
		return true;
	}

	public static void writeUtf8File(String fileName, String fileBody){
		FileOutputStream fos = null;  
		OutputStreamWriter osw = null;
		String fileEncode = System.getProperty("file.encoding"); 
		try {  
			fileBody = fileBody.replaceAll("\\\\\\\\", "\\\\");
	        fos = new FileOutputStream(fileName);  
	        osw = new OutputStreamWriter(fos, "utf-8");  
	        osw.write(fileBody);  
	    } catch (Exception e) {  
	        e.printStackTrace();  
	    }finally{  
	        if(osw!=null){  
	            try {  
	                osw.close();  
	            } catch (IOException e1) {  
	                e1.printStackTrace();  
	            }  
	        }  
	        if(fos!=null){  
	            try {  
	                fos.close();  
	            } catch (IOException e1) {  
	                e1.printStackTrace();  
	            }  
	        }  
	    } 
		return;
	}

	public static boolean writeUtf8File( String fileBody,File out){
		FileOutputStream fos = null;  
		OutputStreamWriter osw = null;
		try {  
			fileBody = fileBody.replaceAll("\\\\\\\\", "\\\\");
			fileBody = removeUTF8BOM(fileBody);
	        fos = new FileOutputStream(out);  
	        osw = new OutputStreamWriter(fos, "utf-8");  
	        osw.write(fileBody);  
	    } catch (Exception e) {  
	        e.printStackTrace();  
	        return false;
	    }finally{  
	        if(osw!=null){  
	            try {  
	                osw.close();  
	            } catch (IOException e1) {  
	                e1.printStackTrace();  
	            }  
	        }  
	        if(fos!=null){  
	            try {  
	                fos.close();  
	            } catch (IOException e1) {  
	                e1.printStackTrace();  
	            }  
	        }  
	    } 
		return true;
	}

	// FEFF because this is the Unicode char represented by the UTF-8 byte order mark (EF BB BF).
	public static final String UTF8_BOM = "\uFEFF";

	public static String removeUTF8BOM(String s) {
	    while (s.startsWith(UTF8_BOM)) {
	        s = s.substring(1);
	    }
	    
	    return s;
	}

}
